package com.sgb.kjwl.view.widget.packers;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.YoungAdapterOnItemClick;
import com.sgb.kjwl.view.ui.adapter.YoungBaseRecycleAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author ypunghow
 * description: 二级筛选
 * @date 2020/12/18
 * first:写在前面,这个界面我们可以实现不同的adapter，只要继承了当前我们所指定的类，这里都可以进行，但是这里其实会发现，虽然我们提供了针对于外部自定义逻辑的一些思路
 * ，但是我们却会发现一个问题，就是封装的问题---局限性，高度封装也就是高度局限，它规定了我们只能在某一个范围内执行某一些代码，其实这样的设计思路是不正确的，虽然能满足
 * 在区域内部的需求，但是对于外部的需求却很难满足，以此类举例，这里我们其实可以进行修改，怎么办?这里我们的父类其实只要一个东西，就是适配器，其余的都可以不要，也就是我们
 * 经常说的设计模式---适配器模式，对于此类来说我们其实也只需要适配器来设置就行了，也就说此类只做显示功能，由子类去写入对应的逻辑，这样才会算是正确的思路，也符合oop的
 * 设计思路
 */
public abstract class TwoLevelForSelectAreaPopWindow<D extends AddressEntity, A extends YoungBaseRecycleAdapter<D>, B extends YoungBaseRecycleAdapter<D>> extends BasePopWindow {

    private A mProvinceAdapter;
    private B mCityAdapter;
    private List<D> mPickSelectEntityList;
    private RecyclerView mRvProvince;
    private RecyclerView mRvCity;
    private String mName;
    private SelectTwoResultClick<D> mSelectResultClick;
    private RecyclerView.LayoutManager mProvinceLayoutManager;
    private RecyclerView.LayoutManager mCityLayoutManager;
    private D province;
    private int mNum;
    private TextView mTvTitle;

    public void setSelectResultClick(SelectTwoResultClick<D> mSelectResultClick) {
        this.mSelectResultClick = mSelectResultClick;
    }

    public TwoLevelForSelectAreaPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    public TwoLevelForSelectAreaPopWindow(Context context, RecyclerView.LayoutManager provinceLayoutManager) {
        super(context, DIR_DOWN_UP);
        this.mProvinceLayoutManager = provinceLayoutManager;
    }

    public TwoLevelForSelectAreaPopWindow(Context context, RecyclerView.LayoutManager provinceLayoutManager, RecyclerView.LayoutManager cityLayoutManager) {
        super(context, DIR_DOWN_UP);
        this.mProvinceLayoutManager = provinceLayoutManager;
        this.mCityLayoutManager = cityLayoutManager;
    }

    @Override
    protected int popLayout() {
        return R.layout.two_level_for_select_area;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mPickSelectEntityList = new ArrayList<>();

        TextView okView = holder.getPop().findViewById(R.id.ok_view);
        TextView cancelView = holder.getPop().findViewById(R.id.cancel_view);

        mTvTitle = holder.getPop().findViewById(R.id.tv_title);
        mRvProvince = holder.getPop().findViewById(R.id.rv_province);
        mRvCity = holder.getPop().findViewById(R.id.rv_city);

        mProvinceAdapter = createProvinceAdapter();
        mCityAdapter = createCityAdapter();

        if (mProvinceLayoutManager != null) {
            mRvProvince.setLayoutManager(mProvinceLayoutManager);
        } else {
            mRvProvince.setLayoutManager(new LinearLayoutManager(mContext));
        }

        if (mCityLayoutManager != null) {
            mRvCity.setLayoutManager(mCityLayoutManager);
        } else {
            mRvCity.setLayoutManager(new LinearLayoutManager(mContext));
        }

        mRvProvince.setAdapter(mProvinceAdapter);
        mRvCity.setAdapter(mCityAdapter);

        okView.setOnClickListener(v -> {
            if (mPickSelectEntityList != null && mPickSelectEntityList.size() > 0){
                mSelectResultClick.result(province,mPickSelectEntityList, mName);
                dissMiss();
            }else {
                MToast.showToast(mContext,"请选择工作区域");
            }
        });

        cancelView.setOnClickListener(v -> {
            mCityAdapter.setCheckDefultDatas(new ArrayList<>(),new SparseBooleanArray());
            mPickSelectEntityList.clear();
            mCityAdapter.setCanSelect(true);
            mSelectResultClick.reset();
        });
    }

    private void setProvinceLayoutManager(RecyclerView.LayoutManager layoutManager) {
        mRvProvince.setLayoutManager(layoutManager);
        mRvProvince.setAdapter(mProvinceAdapter);
    }

    private void setCityLayoutManager(RecyclerView.LayoutManager layoutManager) {
        mRvCity.setLayoutManager(layoutManager);
        mRvCity.setAdapter(mCityAdapter);
    }

    public void setMostSelected(int num) {
        this.mNum = num;
    }

    public void setMultiSelect(boolean multiSelect) {
        mCityAdapter.setMultiSelect(multiSelect);
    }

    /**
     * 设置三级的数据，不联动
     *
     * @param provinceData
     * @param cityData
     */
    public void setNotLinkageTwoLevelData(List<D> provinceData, List<D> cityData) {
        mProvinceAdapter.setDatas(provinceData);
        mCityAdapter.setDatas(cityData);

        mProvinceAdapter.setDefItem(-1);
        mCityAdapter.setDefItem(-1);

        resetNotLinkageItems(provinceData,cityData);
    }

    private void resetNotLinkageItems(List<D> provinceData, List<D> cityData) {
        if (mProvinceAdapter != null){
            mProvinceAdapter.setItemSelect(true);
            mProvinceAdapter.setDatas(provinceData);
            mProvinceAdapter.setOnItemClick(new YoungAdapterOnItemClick<D>() {
                @Override
                public void onItemClick(D d, int position) {
                    province = d;
                }
            });
        }

        if (mCityAdapter != null) {
            mCityAdapter.setDatas(cityData);
            mCityAdapter.setMultiSelect(true);
            mCityAdapter.setDefItem(-1);
            mCityAdapter.setOnItemMultiClick((pickSelectEntities, position) -> {
                mPickSelectEntityList = pickSelectEntities;
                if (pickSelectEntities == null || pickSelectEntities.size() == 0) {
                    mName = "";
                } else {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (D pickSelectEntity : pickSelectEntities) {
                        stringBuilder.append(pickSelectEntity.getName());
                        stringBuilder.append("、");
                    }
                    mName = stringBuilder.substring(0, stringBuilder.length() - 1);
                }
            });
        }
    }

    public void setTwoLevelData(List<D> provinceData, List<List<D>> cityData) {
        mProvinceAdapter.setDatas(provinceData);
        mProvinceAdapter.setDefItem(-1);

        mCityAdapter.setDatas(cityData.get(0));
        mCityAdapter.setDefItem(-1);

        resetItems(provinceData,cityData);
    }

    private void resetItems(List<D> provinceData, List<List<D>> cityData) {
        if (mProvinceAdapter != null){
            mProvinceAdapter.setItemSelect(true);
            mProvinceAdapter.setDatas(provinceData);
            mProvinceAdapter.setOnItemClick(new YoungAdapterOnItemClick<D>() {
                @Override
                public void onItemClick(D d, int position) {

                    mCityAdapter.setCheckDefultDatas(new ArrayList<>(),new SparseBooleanArray());

                    province = d;
                    mCityAdapter.setDatas(cityData.get(position));

                    if (mPickSelectEntityList != null){
                        List<Integer> list = new ArrayList<>();
                        for (int i = 0; i < cityData.get(position).size(); i++) {
                            for (int i1 = 0; i1 < mPickSelectEntityList.size(); i1++) {
                                if (cityData.get(position).get(i).getNo().equals(mPickSelectEntityList.get(i1).getNo())){
                                    list.add(i);
                                }
                            }
                        }

                        Integer[] a = list.toArray(new Integer[0]);
                        int[] b = new int[a.length];
                        for (int i = 0; i < a.length; i++) {
                            b[i] = a[i];
                        }
                        setCityDefaultItems(b);
                    }
                }
            });
        }

        if (mCityAdapter != null) {
            mCityAdapter.setMultiSelect(true);
            mCityAdapter.setDefItem(-1);
            mCityAdapter.setDatas(cityData.get(0));

            if (mPickSelectEntityList != null){
                List<Integer> list = new ArrayList<>();
                for (int i = 0; i < cityData.get(0).size(); i++) {
                    for (int i1 = 0; i1 < mPickSelectEntityList.size(); i1++) {
                        if (cityData.get(0).get(i).getNo().equals(mPickSelectEntityList.get(i1).getNo())){
                            list.add(i);
                        }
                    }
                }

                Integer[] a = list.toArray(new Integer[0]);
                int[] b = new int[a.length];
                for (int i = 0; i < a.length; i++) {
                    b[i] = a[i];
                }
                setCityDefaultItems(b);
            }

            mCityAdapter.setOnItemClick(new YoungAdapterOnItemClick<D>() {
                @Override
                public void onItemClick(D d, int position) {
                    if (!mPickSelectEntityList.contains(d)){
                        mCityAdapter.setCanSelect(mPickSelectEntityList.size() <= 18);

                        mPickSelectEntityList.add(d);
                    }else {
                        mPickSelectEntityList.remove(d);
                        mCityAdapter.setCanSelect(mPickSelectEntityList.size() <= 19);
                    }

                    if (mPickSelectEntityList.size() == 0) {
                        mName = "";
                    } else {
                        StringBuilder stringBuilder = new StringBuilder();
                        for (D pickSelectEntity : mPickSelectEntityList) {
                            stringBuilder.append(pickSelectEntity.getName());
                            stringBuilder.append("、");
                        }
                        mName = stringBuilder.substring(0, stringBuilder.length() - 1);
                    }
                }
            });
        }
    }

    public void setProvinceDefaultItem(int position) {
        mProvinceAdapter.setDefItem(position);
        mProvinceAdapter.setCheckDefault(position);
    }

    public void setProvinceDefaultItems(int position) {
        mProvinceAdapter.setDefItem(position);
        mProvinceAdapter.setCheckDefault(position);
    }

    public void setCityDefaultItem(int position) {
        mCityAdapter.setDefItem(position);
        mCityAdapter.setCheckDefault(position);
    }

    public void setCityDefaultItems(int...position) {
        for (int i : position) {
            mCityAdapter.setDefItem(i);
            mCityAdapter.setCheckDefault(i);
        }
    }

    public void setTitle(String title){
        mTvTitle.setText(title);
    }

    public void setCitySelectItem(List<D> list1){
        if (list1 == null){
            list1 = new ArrayList<>();
        }
        mPickSelectEntityList = list1;

        if (mPickSelectEntityList.size() < 20){
            mCityAdapter.setCanSelect(true);
        }

        mCityAdapter.setCheckDefultDatas(new ArrayList<>(),new SparseBooleanArray());
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < mCityAdapter.getDatas().size(); i++) {
            for (int i1 = 0; i1 < mPickSelectEntityList.size(); i1++) {
                if (mCityAdapter.getDatas().get(i).getNo().equals(mPickSelectEntityList.get(i1).getNo())){
                    list.add(i);
                }
            }
        }

        Integer[] a = list.toArray(new Integer[0]);
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            b[i] = a[i];
        }
        setCityDefaultItems(b);
    }

    protected abstract A createProvinceAdapter();

    protected abstract B createCityAdapter();


    public interface SelectTwoResultClick<D> {

        void result(D province,List<D> pickSelectEntities, String name);
        void reset();
    }
}
